package com.xiaozhengkeji.colastartboot.services;

import com.google.gson.Gson;

import com.xiaozhengkeji.colastartboot.base.entitys.SystemUser;
import com.xiaozhengkeji.colastartboot.base.services.SystemUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.time.LocalDateTime;
import java.util.LinkedList;
import java.util.List;

@Service("SystemUserUtilsService")
public class SystemUserUtilsService {
    SystemUserService systemUserService;

    @Autowired
    public SystemUserUtilsService(SystemUserService systemUserService) {
        this.systemUserService = systemUserService;
    }

    /**
     * 添加一个系统账户
     */
    public Boolean registerSystemUser(String username, String password, String remark) {
        SystemUser systemUser = new SystemUser();
        List<String> userRole = new LinkedList<>();
        userRole.add("user:def");
        systemUser.setUsername(username);
        systemUser.setPassword(password);
        systemUser.setUserRole(new Gson().toJson(userRole));
        systemUser.setRemark(remark);
        systemUser.setUserState(1);
        systemUser.setCreationTime(LocalDateTime.now());
        return systemUserService.insert(systemUser) > 0;
    }

    /**
     * 设置用户状态
     */
    public Boolean setUserState(Integer uid, Integer state) {
        SystemUser systemUser = systemUserService.selectByPrimaryKey(uid);
        systemUser.setUserState(state);
        return systemUserService.updateByPrimaryKey(systemUser) > 0;
    }


}
